package com.hww.dtalk;

import android.util.Log;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.Map;
import java.util.Set;

import gu.dtalk.BaseOption;
import gu.dtalk.BoolOption;
import gu.dtalk.CheckOption;
import gu.dtalk.CmdItem;
import gu.dtalk.DateOption;
import gu.dtalk.ICmdImmediateAdapter;
import gu.dtalk.IPv4Option;
import gu.dtalk.IntOption;
import gu.dtalk.ItemBuilder;
import gu.dtalk.MACOption;
import gu.dtalk.MenuItem;
import gu.dtalk.NumberValidator;
import gu.dtalk.OptionBuilder;
import gu.dtalk.OptionType;
import gu.dtalk.PasswordOption;
import gu.dtalk.RootMenu;
import gu.dtalk.StringOption;
import gu.dtalk.SwitchOption;
import gu.dtalk.event.ValueChangeEvent;
import gu.dtalk.event.ValueListener;
import gu.dtalk.exception.CmdExecutionException;
import gu.dtalk.redis.RedisConfigType;
import gu.simplemq.redis.JedisPoolLazy.PropName;

import static com.google.common.base.Preconditions.checkNotNull;
import static gu.dtalk.engine.DeviceUtils.DEVINFO_PROVIDER;

public class ControlMenu extends RootMenu {

    public ControlMenu() {
    }

    public static final String APPLY = "apply";
    public static final String APPLY_NAME = "name";
    public static final String APPLY_OPEN_DOOR = "open_door";
    public static final String APPLY_THRESHOLD_VALUE = "threshold_value";
    public static final String APPLY_INTERVAL = "interval";

    public static final String FUNCTION = "function";
    public static final String FUNCTION_FACE_CREATE = "face_create";
    public static final String FUNCTION_FACE_CREATE_PASS = "face_create_pass";
    public static final String FUNCTION_FACE_CREATE_PERSON_ID = "face_create_personId";
    public static final String FUNCTION_FACE_CREATE_FACE_ID = "face_create_faceId";
    public static final String FUNCTION_FACE_CREATE_IMG = "face_create_img";


    public ControlMenu init() {
        MenuItem apply =
                ItemBuilder.builder(MenuItem.class)
                        .name(APPLY)
                        .uiName("应用")
                        .addChilds(
                                ItemBuilder.builder(StringOption.class).name(APPLY_NAME).uiName("设备名称").description("用于区分设备所在位置").instance().setValidator(input -> {
                                    Preconditions.checkArgument(Strings.isNullOrEmpty(input), "设备名称不能为空");
                                    return true;
                                }).addListener(new ValueListener<String>() {
                                    @Override
                                    protected void doUpdate(ValueChangeEvent<BaseOption<String>> event) {
                                        /*用于接收数据*/
                                        String value = event.option().getValue();
                                        Log.i("hww ControlMenu", "设备名称  " + value);
                                    }
                                }),
                                ItemBuilder.builder(CmdItem.class).name(APPLY_OPEN_DOOR).uiName("远程开门").description("远程开门").instance().setCmdAdapter(new RelayAdapter()),
                                OptionBuilder.builder(new SwitchOption<Integer>()).name(APPLY_THRESHOLD_VALUE).uiName("阈值设置").description("设置可见光阈值").instance()
                                        .addOption(0, "0.50").addOption(1, "0.60").addOption(2, "0.68").setValidator(input -> {
                                    return true;
                                }).addListener(new ValueListener<Set<Integer>>() {
                                    @Override
                                    protected void doUpdate(ValueChangeEvent<BaseOption<Set<Integer>>> event) {
                                        Set<Integer> value = event.option().getValue();
                                        Log.i("hww ControlMenu", "阈值设置  " + value.toString());
                                    }
                                }),
                                ItemBuilder.builder(IntOption.class).name(APPLY_INTERVAL).uiName("识别间隔时间").description("识别间隔时间[秒],为null或小于3秒均默认为3秒").instance())
                        .instance();
        MenuItem function = ItemBuilder.builder(MenuItem.class).name(FUNCTION).uiName("功能").description("**指定接口功能").addChilds(
                ItemBuilder.builder(CmdItem.class).name(FUNCTION_FACE_CREATE).uiName("添加/更新人脸")
                        .addChilds(
                                OptionType.STRING.builder().name(FUNCTION_FACE_CREATE_PASS).uiName("pass").description("接口安全校验秘钥").instance(),
                                OptionType.STRING.builder().name(FUNCTION_FACE_CREATE_PERSON_ID).uiName("personId").description("人员ID,设备中存在此人才能创建此人的人脸").instance(),
                                OptionType.STRING.builder().name(FUNCTION_FACE_CREATE_FACE_ID).uiName("faceId").description("人脸ID,如果人脸已经存在,则更新").instance(),
                                OptionType.IMAGE.builder().name(FUNCTION_FACE_CREATE_IMG).uiName("imgBase64").description("照片的Base64编码").instance()
                        ).instance().setCmdAdapter(new FaceCreateAdapter())
        ).instance();
        this.addChilds(apply, function);
        return this;
    }

    public ControlMenu register(ValueListener<Object> listener) {
        listener.registerTo(this);
        return this;
    }


    class RelayAdapter implements ICmdImmediateAdapter {
        @Override
        public Object apply(Map<String, Object> input) throws CmdExecutionException {
            return successResponse(0, "开门成功");
        }
    }

    class FaceCreateAdapter implements ICmdImmediateAdapter {
        @Override
        public Object apply(Map<String, Object> input) throws CmdExecutionException {

            return successResponse(0, "返回信息");
        }
    }

    public String successResponse(int code, String msg) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("code", code);
            jsonObject.put("msg", msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }
}
